namespace XEAS.Framework.ShellLib
{
    using System;
    using System.Runtime.InteropServices;

    public class ShellFileOperation
    {
        public string[] DestFiles;
        public FileOperations Operation = FileOperations.FO_COPY;
        public ShellFileOperationFlags OperationFlags = (ShellFileOperationFlags.FOF_WANTNUKEWARNING | ShellFileOperationFlags.FOF_NO_CONNECTED_ELEMENTS | ShellFileOperationFlags.FOF_ALLOWUNDO | ShellFileOperationFlags.FOF_MULTIDESTFILES);
        public IntPtr OwnerWindow = IntPtr.Zero;
        public string ProgressTitle = "";
        public string[] SourceFiles;

        public bool DoOperation()
        {
            ShellApi.SHFILEOPSTRUCT lpFileOp = new ShellApi.SHFILEOPSTRUCT();
            lpFileOp.hwnd = this.OwnerWindow;
            lpFileOp.wFunc = (uint) this.Operation;
            string s = this.StringArrayToMultiString(this.SourceFiles);
            string str2 = this.StringArrayToMultiString(this.DestFiles);
            lpFileOp.pFrom = Marshal.StringToHGlobalUni(s);
            lpFileOp.pTo = Marshal.StringToHGlobalUni(str2);
            lpFileOp.fFlags = (ushort) this.OperationFlags;
            lpFileOp.lpszProgressTitle = this.ProgressTitle;
            lpFileOp.fAnyOperationsAborted = 0;
            lpFileOp.hNameMappings = IntPtr.Zero;
            int num = ShellApi.SHFileOperation(ref lpFileOp);
            ShellApi.SHChangeNotify(0x7fffffff, 3, IntPtr.Zero, IntPtr.Zero);
            if (num != 0)
            {
                return false;
            }
            if (lpFileOp.fAnyOperationsAborted != 0)
            {
                return false;
            }
            return true;
        }

        private string StringArrayToMultiString(string[] stringArray)
        {
            string str = "";
            if (stringArray == null)
            {
                return "";
            }
            for (int i = 0; i < stringArray.Length; i++)
            {
                str = str + stringArray[i] + '\0';
            }
            return (str + '\0');
        }

        public enum FileOperations
        {
            FO_COPY = 2,
            FO_DELETE = 3,
            FO_MOVE = 1,
            FO_RENAME = 4
        }

        [Flags]
        public enum ShellChangeNotificationEvents : uint
        {
            SHCNE_ALLEVENTS = 0x7fffffff,
            SHCNE_ASSOCCHANGED = 0x8000000,
            SHCNE_ATTRIBUTES = 0x800,
            SHCNE_CREATE = 2,
            SHCNE_DELETE = 4,
            SHCNE_DISKEVENTS = 0x2381f,
            SHCNE_DRIVEADD = 0x100,
            SHCNE_DRIVEADDGUI = 0x10000,
            SHCNE_DRIVEREMOVED = 0x80,
            SHCNE_EXTENDED_EVENT = 0x4000000,
            SHCNE_FREESPACE = 0x40000,
            SHCNE_GLOBALEVENTS = 0xc0581e0,
            SHCNE_INTERRUPT = 0x80000000,
            SHCNE_MEDIAINSERTED = 0x20,
            SHCNE_MEDIAREMOVED = 0x40,
            SHCNE_MKDIR = 8,
            SHCNE_NETSHARE = 0x200,
            SHCNE_NETUNSHARE = 0x400,
            SHCNE_RENAMEFOLDER = 0x20000,
            SHCNE_RENAMEITEM = 1,
            SHCNE_RMDIR = 0x10,
            SHCNE_SERVERDISCONNECT = 0x4000,
            SHCNE_UPDATEDIR = 0x1000,
            SHCNE_UPDATEIMAGE = 0x8000,
            SHCNE_UPDATEITEM = 0x2000
        }

        public enum ShellChangeNotificationFlags
        {
            SHCNF_DWORD = 3,
            SHCNF_FLUSH = 0x1000,
            SHCNF_FLUSHNOWAIT = 0x2000,
            SHCNF_IDLIST = 0,
            SHCNF_PATHA = 1,
            SHCNF_PATHW = 5,
            SHCNF_PRINTERA = 2,
            SHCNF_PRINTERW = 6,
            SHCNF_TYPE = 0xff
        }

        [Flags]
        public enum ShellFileOperationFlags
        {
            FOF_ALLOWUNDO = 0x40,
            FOF_CONFIRMMOUSE = 2,
            FOF_FILESONLY = 0x80,
            FOF_MULTIDESTFILES = 1,
            FOF_NO_CONNECTED_ELEMENTS = 0x2000,
            FOF_NOCONFIRMATION = 0x10,
            FOF_NOCONFIRMMKDIR = 0x200,
            FOF_NOCOPYSECURITYATTRIBS = 0x800,
            FOF_NOERRORUI = 0x400,
            FOF_NORECURSEREPARSE = 0x8000,
            FOF_NORECURSION = 0x1000,
            FOF_RENAMEONCOLLISION = 8,
            FOF_SILENT = 4,
            FOF_SIMPLEPROGRESS = 0x100,
            FOF_WANTMAPPINGHANDLE = 0x20,
            FOF_WANTNUKEWARNING = 0x4000
        }
    }
}

